package de.g18.it0a.jlaser.dataio;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URISyntaxException;
import java.net.URL;

import de.g18.it0a.jlaser.domain.RoomConfiguration;
import de.g18.it0a.jlaser.exception.InitializationException;
import de.g18.it0a.jlaser.exception.JLFMalformedFileException;
import de.g18.it0a.jlaser.exception.JLFParseException;
import de.g18.it0a.jlaser.jlf.JLFConverter;


public class RoomConfigurationReader {


    private RoomConfigurationReader() {
        // Prevent instantiation
    }

    public static final RoomConfiguration read(String aFileName) throws FileNotFoundException, JLFMalformedFileException {
        return read(getFile(aFileName));
    }

    private static File getFile(String aFileName) {
        URL url = RoomConfigurationReader.class.getResource(aFileName);
        if (url != null) {
            try {
                return new File(url.toURI());
            } catch (URISyntaxException e) {
                throw new IllegalStateException(e);
            }
        }
        return new File(aFileName);
    }

    public static final RoomConfiguration read(File aFile) throws FileNotFoundException, JLFMalformedFileException {
        try {
            return JLFConverter.valueOf(aFile);
        } catch (InitializationException e) {
            throw new JLFMalformedFileException("Der Inhalt der Datei '" + aFile.getAbsolutePath() + "' ist nicht Standard konform!", e);
        } catch (JLFParseException e) {
            throw new JLFMalformedFileException("Der Inhalt der Datei '" + aFile.getAbsolutePath() + "' ist nicht Standard konform!", e);
        }
    }
}
